package cc.orange.mainView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import cc.orange.BuildConfig;
import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityA12InfoBinding;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.HightEntity;
import cc.orange.entity.NoteExtrEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ScreenUtils;
import cc.orange.utils.ZXToastUtil;
import cc.orange.utils.popup.PopupTSView;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class A12InfoActivity extends BaseActivity implements View.OnClickListener {

    private ActivityA12InfoBinding binding;
    private ZXSharedPrefUtil sharedPrefUtil;
    private NoteExtrEntity entity ;
    private  int intExtra;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPrefUtil = new ZXSharedPrefUtil();
        binding = DataBindingUtil.setContentView(this,R.layout.activity_a12_info);
        intExtra = getIntent().getIntExtra("typs_where", 0);
        if(intExtra == 1){
            getUserDetail(getIntent().getStringExtra("uids"),getIntent().getStringExtra("spares"));
        }else {
            Bundle bb = getIntent().getBundleExtra("note_int");
            entity = bb.getParcelable("note_b");
            initViews();
        }
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.msglistTitle.setLayoutParams(pp);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
        if (BuildConfig.UIA.equals("A1")) {// A1面
            binding.face1TitleBack.setBackgroundResource(R.drawable.icon_a1_titleback);
            binding.infoMsgSend.setBackgroundResource(R.drawable.shape_login_a12_btn);
            binding.infoMsgSend.setTextColor(getResources().getColor(R.color.white));
            binding.msglistTitle2s.setTextColor(getResources().getColor(R.color.white));
            binding.msginfoText1.setTextColor(getResources().getColor(R.color.white));
            binding.msginfoBack .setBackgroundResource(R.drawable.icon_back_img1);

        }else if (BuildConfig.UIA.equals("A2")){// A2面
            binding.face1TitleBack.setBackgroundResource(R.drawable.icon_a2_titleback);
            binding.infoMsgSend.setBackgroundResource(R.drawable.shape_login_btn);
            binding.infoMsgSend.setTextColor(getResources().getColor(R.color.black));
            binding.msglistTitle2s.setTextColor(getResources().getColor(R.color.black));
            binding.msginfoText1.setTextColor(getResources().getColor(R.color.black));
            binding.msginfoBack .setBackgroundResource(R.drawable.icon_back_arrow);
        }
        binding.msginfoBack.setOnClickListener(this);
        binding.msginfoText1.setOnClickListener(this);
        binding.msginfoRel2.setOnClickListener(this);
        binding.msginfoRel3.setOnClickListener(this);
        binding.infoMsgSend.setOnClickListener(this);

        binding.infosEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        RelativeLayout.LayoutParams pp = new RelativeLayout.
                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getStatusBarHeight(A12InfoActivity.this));
        binding.msglistTitle.setLayoutParams(pp);



        binding.infomsgSetimg.setImageResource(entity.getData().getSex() == 0 ?
                R.drawable.icon_msginfo_img2 : R.drawable.icon_msginfo_img1);
        if(!TextUtils.isEmpty(entity.getData().getRemarkName())){
            binding.msgText1.setText(entity.getData().getRemarkName());
        }else{
            binding.msgText1.setText(entity.getData().getName());
        }
        RequestOptions qe = new RequestOptions();
        if(!TextUtils.isEmpty(entity.getData().getHeadPhoto())) {
            qe.error(entity.getData().getHeadPhoto().equals("img_1") ?
                    R.drawable.icon_login_img3 : R.drawable.icon_login_img4);
        }else{
            qe.error(R.drawable.icon_login_img3);
        }
        Glide.with(this).load(entity.getData().getHeadPhoto()).apply(qe).into(binding.infomsgImg1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.msginfo_back://
                String trim = binding.infosEdit.getText().toString().trim();
                if(TextUtils.isEmpty(trim)){
                    finish();
                }else{
                    addAndEdit(trim);
                }
                break;
            case R.id.msginfo_text1://拉黑
                showDia();
                break;
            case R.id.msginfo_rel2://投诉此人
                PopupTSView.showPopupwindow(this, binding.infoMsgSend, new PopupTSView.PopuStatus() {
                    @Override
                    public void popupShow() {

                    }

                    @Override
                    public void popupDismissCancle(String se1) {
                        complaintAdd(se1);
                    }
                });
                break;
            case R.id.msginfo_rel3://清空聊天记录
                chatRemove();
                break;
            case R.id.info_msg_send://发消息
                Intent intent = new Intent(A12InfoActivity.this,A12ConversationListActivity.class);
                intent.putExtra("uid",""+(intExtra == 1 ? entity.getData().getUserId():entity.getData().getId()));
                intent.putExtra("u_name",
                        ""+(TextUtils.isEmpty(entity.getData().getRemarkName()) ?
                                entity.getData().getName() : entity.getData().getRemarkName()));
                intent.putExtra("u_spare",""+entity.getData().getSpareStatus());
                intent.putExtra("type_t", "0");
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        String trim = binding.infosEdit.getText().toString().trim();
        if(TextUtils.isEmpty(trim)){
            finish();
        }else{
            addAndEdit(trim);
        }
    }

    private void showDia() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);       //
        builder.setTitle("");// 设置对话框的标题
        builder.setMessage("确定要拉黑此人吗？");

        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                blockAdd();
            }
        });
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

    private void chatRemove(){
        String s = " { \"sendUserId\": \""+(intExtra == 1 ? entity.getData().getUserId():entity.getData().getId())+"\", \"sendSpareStatus\": \""+entity.getData().getSpareStatus()+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .chatRemove(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(A12InfoActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("消息已清空");
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void complaintAdd(String content){
        String s = " { \"complaintUserId\": \""+(intExtra == 1 ? entity.getData().getUserId():entity.getData().getId())+"\", \"complaintContent\": \""+content+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .complaintAdd(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(A12InfoActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("投诉成功");
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void blockAdd(){
        String s = "{\"blockUserId\": \""+(intExtra == 1 ? entity.getData().getUserId():entity.getData().getId())+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .blockAdd(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(A12InfoActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("拉黑成功");
                            finish();
                            EventBus.getDefault().post(new String("conver_finish"));
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    private void addAndEdit(String trim){
        String s = "{\"remarksContent\": \""+trim+"\",\"remarksUser\": \""+(intExtra == 1 ? entity.getData().getUserId():entity.getData().getId())+"\",\"ramarksSpareStatus\": \""+entity.getData().getSpareStatus()+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .addAndEdit(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(A12InfoActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("修改成功");
                            EventBus.getDefault().post(new String("remarke_"+trim));
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                        finish();
                    }
                });
    }

    private void getUserDetail(String uid,String spares){
        RetrofitFactory.create(ApiService.class)
                .getUserDetail(getTokens(),uid,spares)
                .enqueue(new DefaultCallback<NoteExtrEntity>() {
                    @Override
                    public void onStart(Call<NoteExtrEntity> call) {
                        showLoading_base(A12InfoActivity.this);
                    }

                    @Override
                    public void onError(Call<NoteExtrEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<NoteExtrEntity> call, NoteExtrEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            entity = baseEntity;
                            initViews();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }
}